package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatGoods;
import com.happy.cat.project.system.service.ICatGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 商品Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/goods" )
@Api("商品 API")
public class CatGoodsController extends BaseController {

    private final ICatGoodsService iCatGoodsService;

    /**
     * 查询商品列表
     */
    @ApiOperation(value = "查询商品列表",notes = "查询商品列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatGoods catGoods)
    {
        startPage();
        LambdaQueryWrapper<CatGoods> lqw = new LambdaQueryWrapper<CatGoods>();
        if (catGoods.getGategoryId() != null){
            lqw.eq(CatGoods::getGategoryId ,catGoods.getGategoryId());
        }
        if (StringUtils.isNotBlank(catGoods.getName())){
            lqw.like(CatGoods::getName ,catGoods.getName());
        }
        if (StringUtils.isNotBlank(catGoods.getGoodsImage())){
            lqw.eq(CatGoods::getGoodsImage ,catGoods.getGoodsImage());
        }
        if (StringUtils.isNotBlank(catGoods.getContent())){
            lqw.eq(CatGoods::getContent ,catGoods.getContent());
        }
        if (StringUtils.isNotBlank(catGoods.getExchangeType())){
            lqw.eq(CatGoods::getExchangeType ,catGoods.getExchangeType());
        }
        if (catGoods.getExchangeNumber() != null){
            lqw.eq(CatGoods::getExchangeNumber ,catGoods.getExchangeNumber());
        }
        if (catGoods.getMaxBuy() != null){
            lqw.eq(CatGoods::getMaxBuy ,catGoods.getMaxBuy());
        }
        if (catGoods.getAddedTime() != null){
            lqw.eq(CatGoods::getAddedTime ,catGoods.getAddedTime());
        }
        if (catGoods.getCreateDate() != null){
            lqw.eq(CatGoods::getCreateDate ,catGoods.getCreateDate());
        }
        if (catGoods.getStatus() != null){
            lqw.eq(CatGoods::getStatus ,catGoods.getStatus());
        }
        List<CatGoods> list = iCatGoodsService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @ApiOperation(value = "导出商品列表",notes = "导出商品列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goods:export')" )
    @Log(title = "商品" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatGoods catGoods) {
        LambdaQueryWrapper<CatGoods> lqw = new LambdaQueryWrapper<CatGoods>(catGoods);
        List<CatGoods> list = iCatGoodsService.list(lqw);
        ExcelUtil<CatGoods> util = new ExcelUtil<CatGoods>(CatGoods. class);
        return util.exportExcel(list, "goods" );
    }

    /**
     * 获取商品详细信息
     */
    @ApiOperation(value = "获取商品详细信息",notes = "获取商品详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goods:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatGoodsService.getById(id));
    }

    /**
     * 新增商品
     */
    @ApiOperation(value = "新增商品",notes = "新增商品",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goods:add')" )
    @Log(title = "商品" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatGoods catGoods) {
        catGoods.setCreateDate(new Date());
        catGoods.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatGoodsService.save(catGoods) ? 1 : 0);
    }

    /**
     * 修改商品
     */
    @ApiOperation(value = "修改商品",notes = "修改商品",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goods:edit')" )
    @Log(title = "商品" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatGoods catGoods) {
        catGoods.setCreateDate(new Date());
        catGoods.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatGoodsService.updateById(catGoods) ? 1 : 0);
    }

    /**
     * 删除商品
     */
    @ApiOperation(value = "删除商品",notes = "删除商品",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:goods:remove')" )
    @Log(title = "商品" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatGoodsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
